
#include "EE.h"
#include "save_date.h"
#include "comm.h"
#include "machine_stat.h"
//#include "custom_def.h"
//uint8_t setting_data[14]={1,3,5,1,0,0,0,0,0,0,0,0,0,0};
uint8_t comp_data[6]={0,0,0,0,0,0};
uint8_t Save_Data[6]={0,0,0,0,0,0};
//uint8_t test_Data[6]={0,0,0,0,0,0};
uint8_t save_rdflag;

void save_data_init(void)
{
	uint8_t i=0;
	uint8_t test_number;
	PFDL_Init();
	PFDL_Read(0,comp_data,sizeof(Save_Data));
	//PFDL_Erase(0);
	PFDL_Close();
	//第二次上电。空间里面的值已经被修改过了。
	if(comp_data[0]!=0xff) 
	{
		//memcpy(&Save_Data,comp_data,sizeof(Save_Data));
		//判断一下值是不是对的，不对，那么丢掉
		test_number=checksum(comp_data+1,sizeof(comp_data)-2);
		if(test_number==comp_data[5])
		{
			memcpy(&Save_Data,comp_data,sizeof(Save_Data));
		}
		else
		{
			Save_Data[1]=0;
			Save_Data[2]=heater_info.SetHeatTemp;
			Save_Data[3]=35;
			Save_Data[4]=42;
		}
		save_rdflag=1;
	}
	else 
	{
		
		Save_Data[1]=0;
		Save_Data[2]=heater_info.SetHeatTemp;
		Save_Data[3]=35;
		Save_Data[4]=42;
		Save_Data[5]=checksum(Save_Data+1,sizeof(Save_Data)-2);
		while(i<3)
		{	
			PFDL_Init();
			PFDL_Erase(0);
			PFDL_Write(0,Save_Data,sizeof(Save_Data));
			PFDL_Close();
			PFDL_Init();
			//在读出来，判断是否正确
			PFDL_Read(0,comp_data,sizeof(comp_data));
			PFDL_Close();
			test_number=checksum(comp_data+1,sizeof(comp_data)-2);
			if(test_number==comp_data[5])
			{
				break;
			}
			i++;
		}
		
	}
     
}



void save_data_process(void)
{
//	if(save_wrflag==0) return ;
//		save_wrflag=0;
	uint8_t i=0;
	uint8_t test_number;
	while(i<3)
	{
		PFDL_Init();
		PFDL_Erase(0);
		PFDL_Write(0,Save_Data,sizeof(Save_Data));
		PFDL_Close();

		PFDL_Init();
		//在读出来，判断是否正确
		PFDL_Read(0,comp_data,sizeof(comp_data));
		PFDL_Close();
		test_number=checksum(comp_data+1,sizeof(comp_data)-2);
		if(test_number==comp_data[5])
		{
			break;
		}
		i++;
	} 
	
}